package de.fmannan.addbook.views.actions;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import de.fmannan.addbook.editor.OpenEditorActionDelegate;

/**
 * @author fmannan
 * 
 * Opens a group when a user clicks on a groups context menu.
 */
public class GroupOpenAction implements IObjectActionDelegate {

	public static final String ID = "de.fmannan.addbook.views.actions.GroupOpenAction";
	private static Logger log = Logger.getLogger(ContactDeleteAction.class);
	   /**
     * Memorized active part.
     */
    private IWorkbenchPart activePart;

    /**
     * Memorized active selection.
     */
    private ISelection activeSelection;
    
    /** 
     * Create the action delegate.
     *
     */
    public GroupOpenAction() {
        super();
    }

    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        activePart = targetPart;
    }

    /**
	 * Is executed when the user chooses the option to open a contact via the
	 * contacts popup menu.
	 */
    public void run(IAction action) {
        
        if ( activeSelection instanceof IStructuredSelection) {
            IStructuredSelection structuredSelection = (IStructuredSelection) activeSelection;
            Object firstSelected = structuredSelection.getFirstElement();
            
            // If the selected item is a contact we open the editor
            if ( firstSelected instanceof IAdaptable ) {
            	OpenEditorActionDelegate.openEditor(PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage(),
						activeSelection);
            }
        }        
    }
    
    
    public void selectionChanged(IAction action, ISelection selection) {
        activeSelection = selection;
    }

}
